package com.qing.library_manager.service.Impl.managerImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.library_manager.common.constant.Constant;
import com.qing.library_manager.dao.manager.managerMapper;
import com.qing.library_manager.domain.dto.ManagerInfoDTO;
import com.qing.library_manager.domain.dto.ManagerLoginDTO;
import com.qing.library_manager.domain.dto.ManagerResgisterDTO;
import com.qing.library_manager.domain.entity.Manager;
import com.qing.library_manager.service.manager.manager.managerService;
import com.qing.library_manager.common.util.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>TODO</p>
 *
 * @author Yjx
 * @version 1.0.0
 * @since 2021/11/25
 **/
@Service("managerLogin")
@Transactional
public class managerServiceImpl extends ServiceImpl<managerMapper, Manager> implements managerService {

    //    注入mapper层
    private final managerMapper managerMapper;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    private final SnowFlakeIdWorker snowFlakeIdWorker;
    private final RedisUtil redisUtil;

    public managerServiceImpl(managerMapper managerMapper,
                              BCryptPasswordEncoder bCryptPasswordEncoder,
                              SnowFlakeIdWorker snowFlakeIdWorker,
                              RedisUtil redisUtil) {
        this.managerMapper = managerMapper;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
        this.snowFlakeIdWorker = snowFlakeIdWorker;
        this.redisUtil = redisUtil;
    }

    /**
     * 管理员登录
     *
     * @param managerLoginDTO
     * @return
     */
    @Override
    public ResponseResult mangerLogin(ManagerLoginDTO managerLoginDTO) {
        String usernameDTO = managerLoginDTO.getManagerUsername().trim();
        String passwordDTO = managerLoginDTO.getManagerPassword().trim();

        /**
         * 检验账号存在？
         */
        Manager manager = managerMapper.mangerNameSelect(usernameDTO);
        if (manager == null) {
            return ResponseResult.FAILED("管理员账号不存在");
        }

        /**
         * 匹配密码
         */
        boolean matches = bCryptPasswordEncoder.matches(passwordDTO, manager.getManagerPassword());
        if (!matches) {
            return ResponseResult.FAILED("密码不正确");
        }

        /**
         * 获取管理员信息
         */
        ManagerInfoDTO managerInfoDTO = new ManagerInfoDTO();
        managerInfoDTO.setManagerId(manager.getManagerId());
        managerInfoDTO.setManagerName(manager.getManagerName());
        managerInfoDTO.setManagerPhone(manager.getManagerPhone());

        /**
         *载荷、设置token
         * 将token存放在redis中
         */
        Map<String, Object> claims = ManagerClaimsUtil.managerToClaims(managerInfoDTO);
        String token = JwtUtil.createToken(claims, -1);
        if(!redisUtil.hasKey(Constant.Redis.MANAGER_LOGIN + manager.getManagerUsername())){
            redisUtil.set(Constant.Redis.MANAGER_LOGIN + manager.getManagerUsername(), token, 60 * 10);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        return ResponseResult.SUCCESS(map);
    }

    /**
     * 添加管理员
     *
     * @param managerLoginDTO
     * @return
     */
    @Override
    public ResponseResult managerAdd(ManagerLoginDTO managerLoginDTO) {
        String usernameDTO = managerLoginDTO.getManagerUsername().trim();
        String passwordDTO = managerLoginDTO.getManagerPassword().trim();
        Manager managerFoDB = managerMapper.mangerNameSelect(usernameDTO);
        if (managerFoDB != null) {
            return ResponseResult.FAILED();
        }

        /**
         * 对密码加密
         */
        passwordDTO = bCryptPasswordEncoder.encode(passwordDTO);

        /**
         * 添加管理员信息列表
         */
        Manager manager = new Manager();
        manager.setManagerId(String.valueOf(snowFlakeIdWorker.nextId()));
        manager.setManagerName("默认用户名");
        manager.setManagerPhone(null);
        manager.setManagerUsername(managerLoginDTO.getManagerUsername().trim());
        manager.setManagerPassword(passwordDTO);
        manager.setManagerStatus("1");
        int flag = managerMapper.insert(manager);
        return flag > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

}
